#! /bin/bash
#TD_DATE：昨天日期
SQOOP_HOME=/usr/bin/sqoop
if [[ $1 == "" ]];then
   TD_DATE=`date -d '1 days ago' "+%Y-%m-%d"`
else
   TD_DATE=$1
fi
#上个月1日
Last_Month_DATE=$(date -d "$(date +%Y%m)01 last month" +%Y-%m-01)

#根据TD_DATE计算年季度月日
V_PARYEAR=`date --date="$Last_Month_DATE" +%Y`
V_PARMONTH=`date --date="$Last_Month_DATE" +%m`
V_PARDAY=`date --date="$Last_Month_DATE" +%d`

V_month_for_quarter=`date --date="$Last_Month_DATE" +%-m`
V_PARQUARTER=$(((${V_month_for_quarter}-1)/3+1))

#计算所需要的日期字符串
V_Month="${V_PARYEAR}"_"${V_PARMONTH}"
V_QUARTER="${V_PARYEAR}"_Q"${V_PARQUARTER}"
V_Year="${V_PARYEAR}"

${HIVE_HOME} -S -e "
SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions.pernode=10000;
set hive.exec.max.dynamic.partitions=100000;
set hive.exec.max.created.files=150000;

set hive.enforce.bucketing=true;
set hive.enforce.sorting=true;
set hive.optimize.bucketmapjoin = true;
set hive.auto.convert.sortmerge.join=true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;

insert into itcast_app.customer_signup_app PARTITION (payment_time_year)
select
    signup_num,
    itcast_school_id,
    itcast_school_name,
    itcast_subject_id,
    itcast_subject_name,
    tdepart_id,
    tdepart_name,
    origin_type,
    origin_type_stat,
    payment_time_month, 
    payment_time_day,
    payment_time_hour,
    groupType,
    '1' as time_type, payment_time_year
from itcast_dws.customer_signup_dws  t1
where CONCAT_WS('-',t1.payment_time_year,t1.payment_time_month,t1.payment_time_day) >= '${Last_Month_DATE}'
group by   signup_num,itcast_school_id,itcast_school_name,itcast_subject_id,itcast_subject_name,tdepart_id,tdepart_name,origin_type,origin_type_stat,
     payment_time_yearpayment_time_month, payment_time_day,payment_time_hour,groupType;

--天
insert into itcast_app.customer_signup_app PARTITION (payment_time_year)
select
    signup_num,
    itcast_school_id,
    itcast_school_name,
    itcast_subject_id,
    itcast_subject_name,
    tdepart_id,
    tdepart_name,
    origin_type,
    origin_type_stat,
    payment_time_month, 
    payment_time_day,
    '-1' as payment_time_hour,
    groupType,
    '2' as time_type, payment_time_year
from itcast_dws.customer_signup_dws t1
where CONCAT_WS('-', t1.payment_time_year, t1.payment_time_month, t1.payment_time_day) >= '${Last_Month_DATE}' 
group by   signup_num,itcast_school_id,itcast_school_name,itcast_subject_id,itcast_subject_name,tdepart_id,tdepart_name,origin_type,origin_type_stat,
     payment_time_yearpayment_time_month, payment_time_day,groupType;

--月
insert into itcast_app.customer_signup_app PARTITION (payment_time_year)
select
    signup_num,
    itcast_school_id,
    itcast_school_name,
    itcast_subject_id,
    itcast_subject_name,
    tdepart_id,
    tdepart_name,
    origin_type,
    origin_type_stat,
    payment_time_month, 
    '-1' as payment_time_day,
    '-1' as payment_time_hour,
    groupType,
    '4' as time_type, payment_time_year
from itcast_dws.customer_signup_dws t1
where CONCAT_WS('-', t1.payment_time_year, t1.payment_time_month) >= '${V_Month}' 
group by   signup_num,itcast_school_id,itcast_school_name,itcast_subject_id,itcast_subject_name,tdepart_id,tdepart_name,origin_type,origin_type_stat,
     payment_time_yearpayment_time_month,groupType;


--年
insert into itcast_app.customer_signup_app PARTITION (payment_time_year)
select
    signup_num,
    itcast_school_id,
    itcast_school_name,
    itcast_subject_id,
    itcast_subject_name,
    tdepart_id,
    tdepart_name,
    origin_type,
    origin_type_stat,
    '-1' as payment_time_month, 
    '-1' as payment_time_day,
    '-1' as payment_time_hour,
    groupType,
    '5' as time_type, payment_time_year
from itcast_dws.customer_signup_dws t1
where t1.payment_time_year >= '${V_Year}'
group by   signup_num,itcast_school_id,itcast_school_name,itcast_subject_id,itcast_subject_name,tdepart_id,tdepart_name,origin_type,origin_type_stat,
     payment_time_yearpayment_time_month,groupType;
